﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace SlabInspection
{
    //从n个点中任意取2个点确定直线
    public struct s2Point
    {
        public int p1, p2;
        public s2Point(int p1, int p2)
        {
            this.p1 = p1;
            this.p2 = p2;
        }
    }

    //从n个点中任意取3个点确定平面
    public struct s3Point
    {
        public int p1, p2, p3;
        public s3Point(int p1, int p2, int p3)
        {
            this.p1 = p1;
            this.p2 = p2;
            this.p3 = p3;
        }
    }
    class Utility
    {
        /// <summary>
        /// 组合公式，求从M个点中任意取N个点的所有组合
        /// </summary>
        /// <param name="M"></param>
        /// <param name="N"></param>
        public static ArrayList GetCombination(int M, int N)
        {
            ArrayList _CommList = new ArrayList();
            if (N == 2)
            {
                for (int i = 0; i < M - 1; i++)
                {
                    for (int j = i + 1; j < M; j++)
                    {
                        _CommList.Add(new s2Point(i, j));
                    }
                }
                return _CommList;
            }
            if (N == 3)
            {
                for (int i = 0; i < M - 2; i++)
                {
                    for (int j = i + 1; j < M - 1; j++)
                    {
                        for (int k = j + 1; k < M; k++)
                        {
                            _CommList.Add(new s3Point(i, j, k));
                        }
                    }
                }
                return _CommList;
            }
            return null;
        }

        /// <summary>
        /// 格式化数组所有元素的值，返回一个字符串
        /// </summary>
        /// <param name="ps"></param>
        /// <returns></returns>
        public static string FormatPointsToString(Point3D[] ps)
        {
            Point3D[] points = new Point3D[ps.Length];
            //Array.Copy(ps, points, ps.Length);
            for (int i = 0; i < ps.Length; i++)
            {
                points[i].x = Math.Round(ps[i].x, 2);
                points[i].y = Math.Round(ps[i].y, 2);
                points[i].z = Math.Round(ps[i].z, 2);
            }
            string str = "\n";
            for (int i = 0; i < points.Length; i++)
            {
                if (i % 4 == 0) str += "\n";
                str += i + " {" + points[i].x + "," + points[i].y + "," + points[i].z + "}\t";
            }
            return str;
        }


        public static string FormatPointsToString(Point3D[,] ps)
        {
            Point3D[,] points = new Point3D[ps.GetLength(0), ps.GetLength(1)];
            for (int i = 0; i < points.GetLength(0); i++)
            {
                for (int j = 0; j < points.GetLength(1); j++)
                {
                    points[i, j].x = Math.Round(ps[i, j].x, 2);
                    points[i, j].y = Math.Round(ps[i, j].y, 2);
                    points[i, j].z = Math.Round(ps[i, j].z, 2);
                }
            }
            string str = "\n";
            for (int i = 0; i < points.GetLength(0); i++)
            {
                str += "\n";
                for (int j = 0; j < points.GetLength(1); j++)
                {
                    str += "[" + i + "," + j + "]" + " {" + points[i, j].x + "," + points[i, j].y + "," + points[i, j].z + "}\t";
                }
            }
            return str;
        }

        public static string FormatPointsToString(double[,] ofs)
        {
            double[,] offsets = new double[ofs.GetLength(0), ofs.GetLength(1)];
            for (int i = 0; i < offsets.GetLength(0); i++)
            {
                for (int j = 0; j < offsets.GetLength(1); j++)
                {
                    offsets[i, j] = Math.Round(ofs[i, j], 2);
                }
            }
            string str = "\n";
            for (int i = 0; i < offsets.GetLength(0); i++)
            {
                str += "\n";
                for (int j = 0; j < offsets.GetLength(1); j++)
                {
                    str += "[" + i + "," + j + "]" + " {" + offsets[i, j] + "}\t";
                }
            }
            return str;
        }

        public static string FormatPointsToString(double[] ofs)
        { 
            double[] offsets = new double[ofs.Length];
            for (int i = 0; i < offsets.Length; i++)
            {
                offsets[i] = Math.Round(ofs[i],2);
            }
            string str = "\n";
            for (int i = 0; i < offsets.Length; i++)
            {
                if (i % 4 == 0) str += "\n";
                str += i + " {" + offsets[i] + "}\t";
            }
            return str;
        }
    }
}
